<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <div class="app-space-between">
        <div class="app-space-between-left">
          <el-form-item label="授权用户名:" prop="registeredName">
            <el-input v-model="queryParams.registeredName" placeholder="请输入授权用户名" clearable @clear="handleQuery"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="真实姓名:" prop="realName">
            <el-input v-model="queryParams.realName" placeholder="请输入真实姓名" clearable @clear="handleQuery"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="餐卡卡号:" prop="mealCardNumber">
            <el-input v-model="queryParams.mealCardNumber" placeholder="请输入餐卡卡号" clearable @clear="handleQuery"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="手机号码:" prop="phoneNumber">
            <el-input v-model.number="queryParams.phoneNumber" placeholder="请输入手机号码" clearable @clear="handleQuery"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </div>
        <div class="app-space-between-right">
          <el-form-item>
            <el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
            <el-button size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
      </div>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" :disabled="single" @click="handleUpdate">编辑</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="授权用户名" align="center" prop="postId">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row,'cell’')">{{scope.row.registeredName}}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <el-table-column label="身份证号" align="center">
        <template slot-scope="scope">
            <div>{{scope.row.idNumber | $idNumber}}</div>
        </template>
      </el-table-column>

      <el-table-column label="餐卡卡号" align="center" prop="mealCardNumber" />
      <el-table-column label="注册时间" align="center" prop="creatorTime" />
      <el-table-column label="所属公司" align="center" prop="company" />
      <el-table-column label="AppID" align="center" prop="appid" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body :close-on-click-modal="false">
      <div style="padding-right: 40px;" v-loading="!diLogLoading">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="授权用户名" prop="registeredName">
                <div v-if="details">{{form.registeredName}}</div>
                <el-input v-model="form.registeredName" placeholder="请输入授权用户名" v-else />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="真实姓名:" prop="realName">
                <div v-if="details">{{form.realName}}</div>
                <el-input v-else v-model="form.realName" placeholder="请输入真实姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="手机号码:" prop="phoneNumber">
                <div v-if="details">{{form.phoneNumber}}</div>
                <el-input v-else v-model.number="form.phoneNumber" placeholder="请输入手机号码" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="身份证号:" prop="idNumber">
                <div v-if="details">{{form.idNumber}}</div>
                <el-input v-else v-model="form.idNumber" placeholder="请输入身份证号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="餐卡卡号:" prop="mealCardNumber">
                <div v-if="details">{{form.mealCardNumber}}</div>
                <el-input v-else v-model="form.mealCardNumber" placeholder="请输入餐卡卡号" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="所属公司:" prop="company">
                <div v-if="details">{{form.company}}</div>
                <el-input v-else v-model="form.company" placeholder="请输入所属公司" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属部门:" prop="dept">
                <div v-if="details">{{form.dept}}</div>
                <el-input v-else v-model="form.dept" placeholder="请输入所属部门" />
              </el-form-item>
            </el-col>
          </el-row>


          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="AppID:" prop="appid">
                <div v-if="details">{{form.appid}}</div>
                <el-input v-else v-model="form.appid" placeholder="请输入AppID" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="人员类型:" prop="personnelType">
                <div v-if="details">{{form.personnelType | personnelType}}</div>
                <el-select v-else v-model="form.personnelType" placeholder="请选择人员类型" clearable>
                  <el-option v-for="dict in dict.type.personnel_type" :key="dict.value" :label="dict.label"
                    :value="dict.value" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="头像图片:" prop="portraitPicAddress">
                <image-upload :isShowTip="false" :disabled="details" v-model="form.portraitPicAddress" :limit="1">
                </image-upload>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" v-if="diLogLoading">
        <el-button type="primary" :loading="btnLoading" v-if="!details" @click="submitForm">
          {{form.configId ? '保 存' : '确 定'}}
        </el-button>
        <el-button @click="cancel">{{details? '关 闭' : '取 消'}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listPost,
    getPost,
    delPost,
    addPost,
    updatePost
  } from "@/api/system/post";

  import {
    registeredUserList,
    registeredUserDelete,
    registeredUserDetails,
    registeredUserEdit,
    registeredUserAdd
  } from "@/api/registeredUser/content"
  let _this
  export default {
    dicts: ['personnel_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 岗位表格数据
        postList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        details: false,
        btnLoading: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          phoneNumber: undefined,
          registeredName: undefined,
          realName: undefined,
          mealCardNumber: undefined,
        },
        diLogLoading: undefined,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          postName: [{
            required: true,
            message: "岗位名称不能为空",
            trigger: "blur"
          }],
          postCode: [{
            required: true,
            message: "标题名称不能为空",
            trigger: "blur"
          }],
          postSort: [{
            required: true,
            message: "岗位顺序不能为空",
            trigger: "blur"
          }]
        }
      };
    },
    filters: {
      personnelType(value) {
        let label = `当前没有 "${value}" 字典`
        for (let i in _this.dict.type.personnel_type) {
          if (_this.dict.type.personnel_type[i].value == value) label = _this.dict.type.personnel_type[i].label
        }
        return label
      }
    },
    created() {
      _this = this
      this.getList()
    },
    methods: {
      handleDetails(options) {
        this.show = true
      },
      /** 查询岗位列表 */
      getList() {
        this.loading = true;
        registeredUserList(this.queryParams).then(response => {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        });

      },
      // 取消按钮
      cancel() {
        this.open = false;
      },
      // 表单重置
      reset() {
        this.form = {
          registeredName: '',
          realName: '',
          phoneNumber: '',
          idNumber: '',
          mealCardNumber: '',
          company: '',
          dept: '',
          appid: '',
          personnelType: '',
          portraitPicAddress: '',
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.configId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.title = '新增'
        this.diLogLoading = true
        this.open = true
      },
      /** 修改按钮操作 */
      handleUpdate(row, str) {
        this.reset()
        this.open = true;
        this.details = false
        this.title = '编辑'
        this.diLogLoading = undefined
        if (str) this.details = true
        let id = row.configId || this.ids.toString()
        registeredUserDetails(id).then(res => {
          this.diLogLoading = res.data
          this.form.configId = res.data.configId
          for (let i in this.form) this.form[i] = res.data[i]
        })
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.btnLoading = true
            if (this.form.configId != undefined) {
              registeredUserEdit(this.form).then(response => {
                this.$modal.msgSuccess("编辑成功");
                this.open = false;
                this.getList();
                this.btnLoading = false
              });
            } else {
              registeredUserAdd(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
                this.btnLoading = false
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        let postIds = row.configId || this.ids;
        if (Array.isArray(postIds)) postIds = postIds.toString()
        this.$modal.confirm('此操作将要删除所选数据，是否继续？').then(function() {
          return registeredUserDelete(postIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/post/export', {
          ...this.queryParams
        }, `post_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
